package com.love.driver.service.impl;

import java.util.List;

import com.love.common.core.constant.SecurityConstants;
import com.love.common.core.domain.R;
import com.love.common.core.utils.DateUtils;
import com.love.common.security.utils.SecurityUtils;
import com.love.driver.vo.DriverVo;
import com.love.system.api.RemoteUserService;
import com.love.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.love.driver.mapper.LoveDriverOrderMapper;
import com.love.driver.domain.LoveDriverOrder;
import com.love.driver.service.ILoveDriverOrderService;

/**
 * 代驾订单主Service业务层处理
 * 
 * @author love
 * @date 2025-09-13
 */
@Service
public class LoveDriverOrderServiceImpl implements ILoveDriverOrderService 
{
    @Autowired
    private LoveDriverOrderMapper loveDriverOrderMapper;
    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 查询代驾订单主
     * 
     * @param orderId 代驾订单主主键
     * @return 代驾订单主
     */
    @Override
    public LoveDriverOrder selectLoveDriverOrderByOrderId(Long orderId)
    {
        return loveDriverOrderMapper.selectLoveDriverOrderByOrderId(orderId);
    }
    /**
     * 查询代驾订单主列表
     *
     * @param loveDriverOrder 代驾订单主
     * @return 代驾订单主
     */
    @Override
    public List<LoveDriverOrder> selectLoveDriverOrderList(LoveDriverOrder loveDriverOrder)
    {
        return loveDriverOrderMapper.selectLoveDriverOrderList(loveDriverOrder);
    }

    /**
     * 用户查询代驾订单主列表
     * 
     * @param loveDriverOrder 代驾订单主
     * @return 代驾订单主
     */
    @Override
    public List<LoveDriverOrder> selectUsersLoveDriverOrderList(LoveDriverOrder loveDriverOrder)
    {
        return loveDriverOrderMapper.selectLoveDriverOrderList(loveDriverOrder);
    }

    /**
     * 司机查询代驾订单主列表
     *
     * @param driverCode 代驾订单主
     * @return 代驾订单主
     */
    @Override
    public List<DriverVo> selectDriversLoveDriverOrderList(String driverCode)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Long userId = currentUser.getUserid(); // 获取用户ID
        DriverVo  loveDriverOrderVO = new DriverVo();
        //查询全部订单
        if ("1".equals(driverCode)){
            loveDriverOrderVO.setDriverId(null);
        }else{//查询我的订单
            loveDriverOrderVO.setDriverId(userId);
        }
        List<DriverVo> myDriversList = loveDriverOrderMapper.selectDriversLoveDriverOrderList(loveDriverOrderVO);
        if (!"1".equals(driverCode)) {
            for (DriverVo driverVo : myDriversList) {
                // 查询用户信息
                R<LoginUser> sysUser = remoteUserService.userId(driverVo.getUserId(), SecurityConstants.INNER);
                driverVo.setPhonenumber(sysUser.getData().getSysUser().getPhonenumber());
                driverVo.setEmail(sysUser.getData().getSysUser().getEmail());
            }
        }
        return myDriversList;
    }

    /**
     * 新增代驾订单主
     * 
     * @param loveDriverOrder 代驾订单主
     * @return 结果
     */
    @Override
    public int insertLoveDriverOrder(LoveDriverOrder loveDriverOrder)
    {
        loveDriverOrder.setCreateTime(DateUtils.getNowDate());
        return loveDriverOrderMapper.insertLoveDriverOrder(loveDriverOrder);
    }

    /**
     * 修改代驾订单主
     * 
     * @param loveDriverOrder 代驾订单主
     * @return 结果
     */
    @Override
    public int updateLoveDriverOrder(LoveDriverOrder loveDriverOrder)
    {
        return loveDriverOrderMapper.updateLoveDriverOrder(loveDriverOrder);
    }

    /**
     * 修改代驾订单主
     *
     * @param orderId 代驾订单主
     * @return 结果
     */
    @Override
    public int updateOrderStatus(Long orderId)
    {
        return loveDriverOrderMapper.updateOrderStatus(orderId);
    }

    /**
     * 批量删除代驾订单主
     * 
     * @param orderIds 需要删除的代驾订单主主键
     * @return 结果
     */
    @Override
    public int deleteLoveDriverOrderByOrderIds(Integer[] orderIds)
    {
        return loveDriverOrderMapper.deleteLoveDriverOrderByOrderIds(orderIds);
    }

    /**
     * 删除代驾订单主信息
     * 
     * @param orderId 代驾订单主主键
     * @return 结果
     */
    @Override
    public int deleteLoveDriverOrderByOrderId(Long orderId)
    {
        return loveDriverOrderMapper.deleteLoveDriverOrderByOrderId(orderId);
    }
}
